package top.lixunda.books3.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.books3.dao.RecommendBooksMapper;
import top.lixunda.books3.entity.RecommendBooksExample;
import top.lixunda.books3.model.SimpleBook;
import top.lixunda.books3.service.IndexRecommendService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexRecommendServiceImpl implements IndexRecommendService {

    private final RecommendBooksMapper recommendBooksMapper;

    @Autowired
    public IndexRecommendServiceImpl(RecommendBooksMapper recommendBooksMapper) {
        this.recommendBooksMapper = recommendBooksMapper;
    }

    @Override
    public List<SimpleBook> selectUsingSimpleBooksByType(String type) {
        RecommendBooksExample example = new RecommendBooksExample();
        RecommendBooksExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andAbleEqualTo((byte) 1);
        return recommendBooksMapper.selectSimpleBooksByExample(example);
    }

    @Override
    public List<SimpleBook> selectNormalSimpleBooks() {
        return selectUsingSimpleBooksByType(TYPE_NORMAL);
    }

    @Override
    public List<SimpleBook> selectNewSimpleBooks() {
        return selectUsingSimpleBooksByType(TYPE_NEW);
    }

    @Override
    public List<SimpleBook> selectEconomySimpleBooks() {
        return selectUsingSimpleBooksByType(TYPE_ECONOMY);
    }

    @Override
    public List<SimpleBook> selectLetterSimpleBooks() {
        return selectUsingSimpleBooksByType(TYPE_LETTER);
    }

    @Override
    public List<SimpleBook> selectCultureSimpleBooks() {
        return selectUsingSimpleBooksByType(TYPE_CULTURE);
    }

    @Override
    public List<SimpleBook> selectScienceSimpleBooks() {
        return selectUsingSimpleBooksByType(TYPE_SCIENCE);
    }

    @Override
    public List<SimpleBook> selectLiveSimpleBooks() {
        return selectUsingSimpleBooksByType(TYPE_LIVE);
    }

    @Override
    public List<SimpleBook> selectExamSimpleBooks() {
        return selectUsingSimpleBooksByType(TYPE_EXAM);
    }

    @Override
    public Map<String, List<SimpleBook>> selectAllSimpleBooks() {
        Map<String, List<SimpleBook>> map = new HashMap<>();
        map.put("normal", selectNormalSimpleBooks());
        map.put("new", selectNewSimpleBooks());
        map.put("economy", selectEconomySimpleBooks());
        map.put("letter", selectLetterSimpleBooks());
        map.put("culture", selectCultureSimpleBooks());
        map.put("science", selectScienceSimpleBooks());
        map.put("live", selectLiveSimpleBooks());
        map.put("exam", selectExamSimpleBooks());
        return map;
    }
}
